package com.delivery.www.servlet;

import com.delivery.www.po.Seller;
import com.delivery.www.po.User;

import java.util.Scanner;
import java.util.concurrent.TimeUnit;

import static com.delivery.www.servlet.SellerLoginPage.sellerLogin;
import static com.delivery.www.servlet.UserLoginPage.userLogin;
import static com.delivery.www.servlet.RegisterPage.registerUser;

public class Menu {
    public static void menu() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("============欢迎您！============");
        System.out.println("1.登录（请输入1）");
        System.out.println("2.注册（请输入2）");
        System.out.println("3.退出（请输入3）");
        int i = scanner.nextInt();
        while (i != 1 && i != 2 && i != 3) {
            System.out.println("输入错误！请重新输入");
            i = scanner.nextInt();
        }
        if (i == 1) {
            System.out.println("请输入你的身份：");
            System.out.println("1.商家");
            System.out.println("2.用户");
            int i1 = scanner.nextInt();
            while (i1 != 1 && i1 != 2) {
                System.out.println("输入错误！请重新输入");
                i1 = scanner.nextInt();
            }
            if (i1 == 2) {
                User user = userLogin();
                if (user != null) {
                    System.out.println("登陆成功！即将跳转");
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    UserPage.userPage(user);
                } else {
                    System.out.println("登陆失败，用户名或密码错误");
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    menu();
                }
            } else {
                Seller seller = sellerLogin();
                if (seller != null) {
                    System.out.println("登陆成功！即将跳转");
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    SellerPage.sellerPage(seller);
                } else {
                    System.out.println("登陆失败，用户名或密码错误");
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    menu();
                }
            }
        }
        if (i == 2) {
            boolean b = registerUser();
            if (b) {
                System.out.println("注册成功!即将跳转");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                menu();
            }
        }
        if (i == 3) {
            System.exit(0);
        }
    }
}
